package com.bullsora.kindlerss.model

class AppUser {

  transient springSecurityService
  
  String username
  String password
  boolean enabled
  boolean accountExpired
  boolean accountLocked
  boolean passwordExpired

  String kindleAccount
  String timeZone
  boolean linkedWithFacebook

  static constraints = {
    username blank: false, unique: true
    password blank: false
    kindleAccount(nullable: true)
    timeZone(nullable: true)
  }

  static mapping = {
    password column: '`password`'
    /*id generator: 'uuid'*/
  }

  Set<Role> getAuthorities() {
    AppUserRole.findAllByAppUser(this).collect { it.role } as Set
  }

  def beforeInsert() {
    encodePassword()
  }

  def beforeUpdate() {
    if (isDirty('password')) {
      encodePassword()
    }
  }

  protected void encodePassword() {
    password = springSecurityService.encodePassword(password, username)
  }
}
